//节点
class Node{
    constructor(element){
        this.element = element;
        this.next = null;
    }
}

//循环链表
class CircleList{
    constructor(){
        this.head = null; //记录第一个元素
        this.count = 0;   //链表的节点个数
    }

    getElementAt(index){
        let current = this.head;
        for(let i = 0;i<index;i++){
            current = current.next;
        }
        return current
    }

    //添加
    push(element){
        let node = new Node(element);
        //是否是第一次添加
        if(this.head === null){
            this.head = node;
            node.next = this.head;
        }else{
            let last = this.getElementAt(this.count-1);
            last.next = node;
            node.next = this.head;
        }
        this.count++;
    }

    //n  : 多少人  k；报数的上限
    ysfCircle(n,k){
        //1.把n个人放到循环链表内
        for(let i = 1;i<=n;i++){
            this.push(i);
        }

        let current = this.head;
        let helper = this.head; //帮助指针 永远指向的是current的前一个节点

        while(helper.next != this.head){
            helper = helper.next;
        }
        //循环停止后，helper指向current的前一个节点

        while(this.count > 15){
            for(let a = 1;a<k;a++){ //9
                current = current.next;
                helper = helper.next;
            }
            console.log("要淘汰的人",current.element);
            helper.next = current.next;
            current = current.next;
            this.count--;
        }
    }
}

let c = new CircleList();

c.ysfCircle(30,9);

console.log(c);

